/* file name  : JSlingMessage.java
 * created    : Fri 23 Sep 2005 10:27:21 PM EDT
 * authors    : Saikat Guha <saikat@cs.cornell.edu>
 * copyright  : GNU Lesser General Public License version 2.1 or later
 * 
 * Copyright (C) 2005 Saikat Guha <saikat@cs.cornell.edu>
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 * 
 */
package net.nutss.signaling;

import net.nutss.*;
import java.io.Serializable;
import java.net.URI;
/**
 * Encapsulates user data and adds routing information for the proxy network
 *
 * @author Saikat Guha
 * @version %I%, %G%
 */
public class JSlingMessage implements SignalingMessage, Serializable {

    /**
    * Message type
    */
    enum Type {

        /** Registration related message */
        JSLING_REGISTER,

        /** Message sent from a source, to be sent to a particular destination
         * through multi-hop routing
         */
        JSLING_ROUTE,

        /** Message sent from a proxy, to be sent to the destination in one-hop */
        JSLING_FORWARD
    };

    /**
     * Response to proxied messages
     */
    enum Response {

        /** Message was not forwarded or process */
        JSLING_ERROR,

        /** Message was accepted */
        JSLING_SUCCESS
    };

    /**
     * Message type
     */
    Type type;

    /**
     * Response from proxy network
     */
    Response response;

    /**
     * Source
     */
    URI source;

    /**
     * Intended destination
     */
    URI destination;

    /**
     * Message ID
     */
    int id;

    /**
     * Message user data
     */
    Object payload;

    static final long serialVersionUID = 0;

    /**
     * Serialized version of the message
     */
    transient byte[] bbuf;

    /**
     * Message id of last message created. Used to stamp messages with unique id with wraparound.
     */
    transient private static int last_id = (int)(Math.random() * Integer.MAX_VALUE);

    /**
     * Creates a new message
     * @param src  source of message
     * @param dst  destination of message
     * @param payload message contents
     */
    public JSlingMessage(URI src, URI dst, Object payload) {
        this.source = src;
        this.destination = dst;
        this.payload = payload;
        this.id = last_id++;
        this.type = Type.JSLING_ROUTE;
        /* commented out by casper
        this.response=Response.JSLING_SUCCESS;
        */
    }

    public boolean isError() { return response == Response.JSLING_ERROR; }

    public boolean hasPayload() { return payload != null; }

    public URI getSource() { return source; }

    public URI getDestination() { return destination; }

    public Object getPayload() { return payload; }

    public int hashCode() {
        // Must match all messages for the same signaling session going
        // either way
        return source.hashCode() ^ destination.hashCode() ^ id;
    }

    public boolean equals(Object o) {
        if (o instanceof JSlingMessage) {
            // Must match all messages for the same signaling session going
            // either way
            JSlingMessage msg = (JSlingMessage) o;
            return ((source.equals(msg.source) && destination.equals(msg.destination)) ||
                    (source.equals(msg.destination) && destination.equals(msg.source)))
                && id == msg.id;
        } else {
            return false;
        }
    }

    public String toString() {
        return String.format("{type=%s response=%s src=%s dst=%s id=%d payload=%s}",
                type, response, source, destination, id, payload);
    }

}
